"""
以下代码由AI生成
"""
import os
import random
import colorama
import pygame

# 初始化 pygame
pygame.init()

# 定义游戏常量
WIDTH = 9
HEIGHT = 9
MINE_COUNT = 10

# 定义颜色常量
colorama.init()
RESET = colorama.Style.RESET_ALL
RED = colorama.Fore.RED
GREEN = colorama.Fore.GREEN
YELLOW = colorama.Fore.YELLOW
BLUE = colorama.Fore.BLUE
MAGENTA = colorama.Fore.MAGENTA
CYAN = colorama.Fore.CYAN
WHITE = colorama.Fore.WHITE


# 定义游戏类
class Minesweeper:
    def __init__(self):
        self.board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
        self.mines = []
        self.revealed = [[False for _ in range(WIDTH)] for _ in range(HEIGHT)]
        self.flags = [[False for _ in range(WIDTH)] for _ in range(HEIGHT)]
        self.game_over = False
        self.victory = False

        # 生成地雷
        for i in range(MINE_COUNT):
            x = random.randint(0, WIDTH - 1)
            y = random.randint(0, HEIGHT - 1)
            if (x, y) not in self.mines:
                self.mines.append((x, y))

        # 计算数字
        for x in range(WIDTH):
            for y in range(HEIGHT):
                if (x, y) not in self.mines:
                    count = 0
                    for dx in range(-1, 2):
                        for dy in range(-1, 2):
                            if 0 <= x + dx < WIDTH and 0 <= y + dy < HEIGHT and (x + dx, y + dy) in self.mines:
                                count += 1
                    self.board[y][x] = count

    def reveal(self, x, y):
        if self.game_over or self.victory:
            return

        if self.flags[y][x]:
            return

        if (x, y) in self.mines:
            self.game_over = True
            return

        self.revealed[y][x] = True

        if self.board[y][x] == 0:
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    self.reveal(x + dx, y + dy)

        if all([all(row) for row in self.revealed]) and not self.game_over:
            self.victory = True

    def toggle_flag(self, x, y):
        if self.game_over or self.victory:
            return

        self.flags[y][x] = not self.flags[y][x]

    def draw(self):
        os.system("cls")
        print(" " * 4 + " ".join([str(i) for i in range(WIDTH)]))
        for y in range(HEIGHT):
            row = "{:<4}".format(y)
            for x in range(WIDTH):
                if self.revealed[y][x]:
                    if (x, y) in self.mines:
                        row += RED + "*" + RESET
                    else:
                        row += str(self.board[y][x])
                elif self.flags[y][x]:
                    row += BLUE + "*" + RESET
                else:
                    row += " "
            print(row)

        if self.game_over:
            print("Game over!")
        elif self.victory:
            print("You win!")

    def play(self):
        while not self.game_over and not self.victory:
            self.draw()
            command = input("Enter command (reveal/flag/quit): ")
            if command == "quit":
                break
            try:
                x, y, action = command.split()
                x, y = int(x), int(y)
                if action == "reveal":
                    self.reveal(x, y)
                elif action == "flag":
                    self.toggle_flag(x, y)
            except:
                print("Invalid command")

        pygame.quit()


# 主循环
game = Minesweeper()
game.play()
